<?php 
// invoicing.thtml
$this->pageTitle = "Check In";
?>
<h1>All Orders by <?=ucwords($sorting[0])?></h1>
<div class="breadcrumbs">
<p>View By: <?=$html->link(ucwords($sorting[1]), '/producers/checkIn/'. $producer['Producer']['producer_id'] . '/' . $sorting[1])?> | <?=$html->link(ucwords($sorting[0]), '/producers/checkIn/'. $producer['Producer']['producer_id'] . '/' . $sorting[0])?></p>
</div>
<div class="message">
<h4>Tips:</h4>
<p> - Save often!	The save button is at the bottom of the page.	If you have a long list of items, it's good to save often so the site doesn't automatically log you out.</p>
<p> - Don't use the browser back button!	Your browser's back button may cause odd behavior of this page.<p>
<p> - If you have an issue, or need to remove or add an entry, contact the Tech Guy at pauldl@gmail.com. </p>
<p> - When you're done, check the box that says "Go to Invoice" and Save.	You'll be sent to your new invoice, with the weights tabulated.</p>
</div>
<form method="post" action='<?=$html->url('/producers/checkIn/'. $producer['Producer']['producer_id'] . '/' . $sorting[0]);?>'>
<table>
<tr>
	<th><?=ucwords($sorting[1])?></th><th>Quantity</th><th>Price ea.</th><td>Weight ea. (est)</td><th>Total</th>
<?php 
if($current_user['auth_type'] == 'administrator') {
	echo '<th>Actions</th>';
}
?>
</tr>
<?php
$p = '';
$m = '';
$total = 0;
foreach ($lines as $line) {
	for($i = 1; $i <= $line['basket_items']['quantity']; $i++) {
		$line["member_name"] = $line['member']['last_name'] . ", " . $line['member']['first_name'];
		$line["product_name"] = $line['product_list']['product_name'];
		if ($line['basket_items']['weight'] != 0 or $i == 1) {
			$q = ($line['basket_items']['weight'] == 0) ? $line['basket_items']['quantity'] : 1;
			if ($p != $line[$sorting[0] . '_name']) {
				$p = $line[$sorting[0] . '_name'];
				echo '<tr><th colspan="6">' . ucwords($p) . '</th></tr>' . "\n";
			}
			if ($m != $line[$sorting[1] . '_name']) {
				$m = $line[$sorting[1] . '_name'];
			//	echo '<tr><th colspan="4">' . ucwords($m) . '</th></tr>' . "\n";
			}
			
			$w = ($line['basket_items']['weight'] == 0) ? 'N/A' : $form->input('BasketItem.w_' . $line['basket_items']['id'] . '_' . $i,array('label' => '', 'value' => $line['basket_items']['weight'],'class'=>'quantity')) . ' ' . $line['orderables']['priced_by'] . '(s)';
			
			$line['total'] = ($line['basket_items']['missing']) ? 0 : $line['orderables']['price'] * $line['basket_items']['quantity'];
			$line['total'] = ($line['basket_items']['weight'] == 0) ? $line['total'] : $line['total'] * $line['basket_items']['weight'];
			$line['tax'] = ($line['product_list']['tax_exempt']) ? 0 : $line['total'] * SALES_TAX;
			echo '<!-- ' . $line['basket_items']['id'] . '-->';
			echo '<td>' . ucwords($m) . '</td>';
			if ($current_user['auth_type'] == AUTH_ADMIN) {
				echo '<td>', $form->input('BasketItem.q_' . $line['basket_items']['id'] . '_' . $i, array('label' => '', 'value' => $line['basket_items']['quantity'],'class'=>'quantity')), $line['orderables']['ordered_by'], '</td>';
			} else {
				echo '<td>' . $q . ' ' . $line['orderables']['ordered_by'] . '</td>';
			}
			echo '<td>$' . number_format($line['orderables']['price'],2) . ' per ' . $line['orderables']['priced_by'] . '</td>';
			echo '<td>' . $w . '</td>';
			echo '<td>$' . number_format($line['total']+$line['tax'],2) . "</td>";
		if($current_user['auth_type'] == 'administrator') {
			if ($line['basket_items']['missing']) {
				echo '<td>' . $html->link('[mark as delivered]', '/basketItems/markDelivered/' . $line['basket_items']['id']) . '</td>';
			} else {
				echo '<td>' . $html->link('[mark as missing]', '/basketItems/markMissing/' . $line['basket_items']['id']) . '</td>';
			}
		}
			echo "</tr>\n";
			$total += $line['total']+$line['tax'];
		}
	}
}
?>
</table>
<p><label for="Producer/redirect">Go to Invoice</label><?=$form->checkbox('Producer.redirect');?></p>
<?=$form->submit('Save Weights');?>
</form>
